﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Main_News : System.Web.UI.Page
{
    EncriptManager em = new EncriptManager();
    NewsDao nd = new NewsDao();
    NewsVo nv = new NewsVo();
    DataTable dt = null;
    UsersDao ud = new UsersDao();
    UsersVo uv = new UsersVo();
    OpinionDao od = new OpinionDao();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            try
            {
                if (em.Decript(Request.QueryString["nid"]) == null || Request.QueryString["nid"] == null)
                {
                    Response.Redirect("../index.aspx");
                }
            }
            catch
            {
                Response.Redirect("../index.aspx");
            }
            string nid = em.Decript(Request.QueryString["nid"]);
            try
            {
                int id = Convert.ToInt32(nid);
            }
            catch
            {
                Response.Redirect("../index.aspx");
            }
            int page = 1;
            if (nid == "")
            {
                Response.Redirect("../index.aspx");
            }
            if (Request.QueryString["page"] != null && Request.QueryString["page"] != "1")
            {
                try
                {
                    page = Convert.ToInt32(Request.QueryString["page"].Trim());
                }
                catch
                {
                    Response.Redirect("../index.aspx");
                }
            }
            uv = (UsersVo)Session["user"];
            if (uv != null)
            {
                labname.Visible = false;
                txtname.Visible = false;
            }
            nv = nd.getSingleNews(nid);
            if (nv != null)
            {
                string text = nv.Ncontent;
                //设置Title和搜索关键词
                Page.Title = nv.Nmaintitle + "-" + ConfigurationSettings.AppSettings["title"].ToString() + "-本地信息 热闹集市 原创心情 教育天地 艺术地带";
                HtmlMeta metaKeyWords = new HtmlMeta();
                HtmlMeta metaDescription = new HtmlMeta();
                metaKeyWords.Name = "Keywords";
                metaKeyWords.Content = nv.Nmaintitle;
                metaDescription.Name = "description";
                while (nv.Ncontent.IndexOf("<") >= 0 && nv.Ncontent.IndexOf(">") >= 0)
                {
                    int start = nv.Ncontent.IndexOf("<");
                    int end = nv.Ncontent.IndexOf(">");
                    if (start > end)
                    {
                        nv.Ncontent = nv.Ncontent.Replace(">", "");
                    }
                    else
                    {
                        string rep = nv.Ncontent.Substring(start, end - start + 1);
                        nv.Ncontent = nv.Ncontent.Replace(rep, "");
                    }
                    nv.Ncontent = nv.Ncontent.Replace("&nbsp;", "");
                    nv.Ncontent = nv.Ncontent.Replace("&#8212;", "—");
                    nv.Ncontent = nv.Ncontent.Replace("&#8216;", "‘");
                    nv.Ncontent = nv.Ncontent.Replace("&#8217;", "’");
                    nv.Ncontent = nv.Ncontent.Replace("&#8220;", "“");
                    nv.Ncontent = nv.Ncontent.Replace("&#8221;", "”");
                    nv.Ncontent = nv.Ncontent.Replace("&#8230;", "…");
                }
                if (nv.Ncontent.Length > 100)
                {
                    metaDescription.Content = nv.Ncontent.Substring(0, 100) + ".....";
                }
                else
                {
                    metaDescription.Content = nv.Ncontent + ".....";
                }

                Header.Controls.Add(metaKeyWords);
                Header.Controls.Add(metaDescription);
                labcontent.Text = nv.Ncontent;
                labmaintitle.Text = nv.Nmaintitle;
                labtime.Text = Convert.ToDateTime(nv.Ntime).ToString("g"); ;
                string content = text;
                ArrayList body = new ArrayList();
                nid = em.Encript(nid);
                hlopiniondown.NavigateUrl = "Opinion.aspx?nid=" + nid;
                string PageStr = "News.aspx?nid=" + nid + "&";
                if (content.IndexOf("{$Page$}") > -1)
                {
                    string[] tempList = content.Split(new string[] { "{$Page$}" }, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < tempList.Length; i++)
                    {
                        body.Add(tempList[i]);
                    }
                }
                else
                {
                    body.Add(content);
                }
                if (page < 1 || page > body.Count)
                {
                    page = 1;
                }
                if (body.Count == 1)
                {
                    labcontent.Text = (string)body[0];
                }
                else
                {
                    if (page <= body.Count)
                    {
                        labcontent.Text = (string)body[page - 1];
                    }
                    else
                    {
                        labcontent.Text = (string)body[0];
                    }
                }
                l_page.Text = nd.PageInfo(body.Count, 1, page, PageStr, "页");
                //nd.updateNewsCount(em.Decript(Request.QueryString["nid"]));//增加浏览次数
                dt = od.getOpinion(em.Decript(Request.QueryString["nid"]), "0");
                labnumber.Text = dt.Rows.Count.ToString();
            }
        }
    }
    protected void btntijiao_Click(object sender, EventArgs e)
    {
        string nid = em.Decript(Request.QueryString["nid"]);
        string Nid = Request.QueryString["nid"];
        if (txtcontent.Text == "")
        {
            string script = "<script>alert('回复内容不能为空！');</script>";
            Page.RegisterStartupScript("", script);
        }
        else
        {
            OpinionVo ov = new OpinionVo();
            ov.Ocontent = txtcontent.Text.Replace(" ", "&nbsp;&nbsp;&nbsp;").Replace("\r\n", "<br>"); ;
            ov.Id = Convert.ToInt32(nid);
            ov.Otime = DateTime.Now.ToString("g");
            uv = (UsersVo)Session["user"];
            if (uv != null)
            {
                ov.Uid = uv.Uid;
                ov.Oauthor = uv.Unickname;
            }
            else
            {
                ov.Uid = 0;
                if (txtname.Text == "")
                {
                    ov.Oauthor = "纺织城网友";
                }
                else
                {
                    ov.Oauthor = txtname.Text;
                }
            }
            ov.Otype = 0;
            Exception ex = od.addOpinion(ov);
            if (ex == null)
            {
                string script = "<script>alert('评论成功！');location.href='Opinion.aspx?nid=" + Nid + "';</script>";
                Page.RegisterClientScriptBlock("", script);
            }
            else
            {
                string script = "<script>alert('评论失败！');</script>";
                Page.RegisterStartupScript("", script);
            }
        }
    }
}
